﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Client.Models;
using Newtonsoft.Json;

namespace Client.Base
{
    public partial class FrmCustomer : DevExpress.XtraEditors.XtraForm
    {
        public FrmCustomer()
        {
            InitializeComponent();
        }
        private int Sid;
        private int SUid;
        private string SParentCode;
        private string SCode;
        private string SSimpCode;
        private string SCname;
        private string STaxCode;
        private string SEmail;
        private string SAddress_Tel;
        private string SBankNumber;
        private string SRemark;
        /// <summary>
        /// 载入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmCustomer_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = null;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.uom.Token);
                CustomerModel cm = new CustomerModel();
                cm.CName = tbxCname.Text.Trim();
                cm.Bank_Number = tbxBank.Text.Trim();
                cm.Address_Tel = tbxAddress.Text.Trim();
                cm.TaxCode = tbxTaxNo.Text.Trim();
                cm.Email = tbxEmail.Text.Trim();
                cm.Remark = tbxRemark.Text.Trim();
                RetModel rm = Share.PostData("51API/Base/CustomerAdd", dc, JsonConvert.SerializeObject(cm));
                if (rm.Success)
                {
                    MessageBox.Show("增加成功！");
                    tbxCname.Text = "";
                    tbxAddress.Text = "";
                    tbxTaxNo.Text = "";
                    tbxBank.Text = "";
                    tbxEmail.Text = "";
                    tbxRemark.Text = "";
                    barButtonItem4_ItemClick(sender, e);
                    return;
                }
                else
                {
                    MessageBox.Show("增加失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.uom.Token);
                CustomerModel cm = new CustomerModel();
                cm.id = Sid;
                cm.CName = tbxCname.Text.Trim();
                cm.TaxCode = tbxTaxNo.Text.Trim();
                cm.Address_Tel = tbxAddress.Text.Trim();
                cm.Bank_Number = tbxBank.Text.Trim();
                cm.Email = tbxEmail.Text.Trim();
                cm.Remark = tbxRemark.Text.Trim();
                RetModel rm = Share.PostData("51API/Base/CustomerEdit", dc, JsonConvert.SerializeObject(cm));
                if (rm.Success)
                {
                    MessageBox.Show("修改成功！");
                    tbxCname.Text = "";
                    tbxAddress.Text = "";
                    tbxTaxNo.Text = "";
                    tbxBank.Text = "";
                    tbxEmail.Text = "";
                    tbxRemark.Text = "";
                    barButtonItem4_ItemClick(sender, e);
                    return;
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.uom.Token);
                dc.Add("id", Sid.ToString());
                RetModel rm = Share.PostData("51API/Base/CustomerDel", dc, null);
                if (rm.Success)
                {
                    MessageBox.Show("删除成功！");
                    tbxCname.Text = "";
                    tbxAddress.Text = "";
                    tbxTaxNo.Text = "";
                    tbxBank.Text = "";
                    tbxEmail.Text = "";
                    tbxRemark.Text = "";
                    barButtonItem4_ItemClick(sender, e);
                    return;
                }
                else
                {
                    MessageBox.Show("删除失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                gridControl1.DataSource = null;
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.uom.Token);
                RetModel rm = Share.PostData("51API/Base/GetCustomerByLikeName", dc, null);
                if (rm.Success)
                {
                    CustomerModel[] rcm = JsonConvert.DeserializeObject<CustomerModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("uid");
                    dt.Columns.Add("上级编码");
                    dt.Columns.Add("编码");
                    dt.Columns.Add("简码");
                    dt.Columns.Add("客户名称");
                    dt.Columns.Add("税号");
                    dt.Columns.Add("电子邮件");
                    dt.Columns.Add("地址电话");
                    dt.Columns.Add("银行账号");
                    dt.Columns.Add("备注");
                    for (int i = 0; i < rcm.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = rcm[i].id;
                        dr["uid"] = rcm[i].uid;
                        dr["上级编码"] = rcm[i].ParentCode;
                        dr["编码"] = rcm[i].Code;
                        dr["简码"] = rcm[i].SimpCode;
                        dr["客户名称"] = rcm[i].CName;
                        dr["税号"] = rcm[i].TaxCode;
                        dr["电子邮件"] = rcm[i].Email;
                        dr["地址电话"] = rcm[i].Address_Tel;
                        dr["银行账号"] = rcm[i].Bank_Number;
                        dr["备注"] = rcm[i].Remark;
                        dt.Rows.Add(dr);
                    }
                    gridControl1.DataSource = dt;
                    gridView1.Columns["id"].Visible = false;
                    gridView1.Columns["uid"].Visible = false;
                    gridView1.Columns["上级编码"].Visible = false;
                    gridView1.Columns["简码"].Visible = false;

                }
                else
                {
                    MessageBox.Show("查询失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 点击列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                tbxCname.Text = "";
                tbxAddress.Text = "";
                tbxTaxNo.Text = "";
                tbxBank.Text = "";
                tbxEmail.Text = "";
                tbxRemark.Text = "";

                Sid = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["id"]));
                SCname = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["客户名称"]).ToString();
                SCode = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["编码"]).ToString();
                STaxCode = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["税号"]).ToString();
                SAddress_Tel = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["地址电话"]).ToString();
                SEmail = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["电子邮件"]).ToString();
                SBankNumber = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["银行账号"]).ToString();
                SRemark = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["备注"]).ToString();

                tbxCname.Text = SCname;
                tbxAddress.Text = SAddress_Tel;
                tbxTaxNo.Text = STaxCode;
                tbxBank.Text = SBankNumber;
                tbxEmail.Text = SEmail;
                tbxRemark.Text = SRemark;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}